if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");

var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!")
    while (true) {
      var allow = text("允许").findOne(5000)
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!")
        allow.click()
        sleep(1000)
        thread.interrupt()
        break
      }
    }
  })
  sleep(1000)
  var result = launchApp(app_name)
  sleep(3000)
  thread.interrupt()
  return result
}

 
// 随机向上滑动
function swip_up() {
  var w = device.width
  var h = device.height

  function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = { "x": 0, "y": 0 };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
  };


  function sml_move(qx, qy, zx, zy, time) {
    //仿真随机带曲线滑动: qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
    var xxy = [time];
    var point = [
      { "x": qx, "y": qy },
      { "x": random(qx - 100, qx + 100), "y": random(qy, qy + 50) },
      { "x": random(zx - 100, zx + 100), "y": random(zy, zy + 50) },
      { "x": zx, "y": zy }
    ];
    for (let i = 0; i < 1; i += 0.08) {
      xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]
      xxy.push(xxyy);
    }
    gesture.apply(null, xxy);
  }

  sml_move(w / 2, h * 0.8 + h * random() * 0.1, w / 2, h * 0.15, 200 + 600 * random());
}

function 看视频(x) {
  var 视频 = text(x).findOne(3000)
  if (视频) {
    toastLog('观看' + x + '视频')
    var finalActivity = currentActivity();
    var weight = 视频.parent().bounds()
    click(weight.centerX(), weight.centerY())
    sleep(5000)
    toastLog('当前是广告页面')
    utils.waitAdvert(finalActivity);
    var ivClose = id('ivClose').findOne(2000)
    if (ivClose) {
      log("关闭弹框.")
      ivClose.click()
      sleep(time)
    }
  }
}

function task() {
  var 任务 = text('任务').findOne(60000)
  if (任务) {
    任务.parent().click()
    sleep(5000)
    var len = ["第一个","第二个","第三个","第四个","第五个","第六个","第七个"]
    for(var idx = 0;idx<len.length ; idx++){
      utils.updateAppProgress(idx * 10);
      看视频(len[idx]);
    }
    swip_up()
    sleep(5000)
    var 基础任务 = text('基础任务').findOne(2000)
    arr = 基础任务.parent().parent().find(text('去完成'))
    if (arr.length > 0) {
      var 浏览商品 = text('浏览商品').findOne(2000)
      arr = 浏览商品.parent().parent().find(text('去完成'))
      if (arr.length > 0) {
        arr[0].click()
        toastLog('浏览商品')
        sleep(time)
        swip_up()
        sleep(random(15000, 17000))
        back()
      }
      utils.updateAppProgress(80);
      var 查看视频 = text('查看视频').findOne(2000)
      arr = 查看视频.parent().parent().find(text('去完成'))
      if (arr.length > 0) {
        var finalActivity = currentActivity();
        arr[0].click()
        toastLog('当前是广告界面')
        utils.waitAdvert(finalActivity);
        sleep(time)
      }
      utils.updateAppProgress(90);
      var 查看商家企业预入驻 = text('查看商家企业预入驻').findOne(2000)
      arr = 查看商家企业预入驻.parent().parent().find(text('去完成'))
      if (arr.length > 0) {
        arr[0].click()
        sleep(time)
        back()
      }      
    } else {
      toastLog('已经完成任务')
    }
  }else{
    console.log("获取任务按钮失败");
    
  }
}

function main() {
  let open_app = launchAppEnhance("链淘");
  if (open_app) {
    var thread_1 = threads.start(function () {
      toastLog("检查广告弹窗")
      while (true) {
        var widget = id("tvCancel").findOne(2000);
        if (widget) {
          widget.click()
        }
      }
    })
    task()
    thread_1.interrupt()
  }
  else {
    toastLog("链淘打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}

storage.put("script_running_status", "end");
